﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Medical_Store_Billing.Business_Login_Layer;

namespace Medical_Store_Billing
{
    public partial class FormXuathang : Form
    {
        DathangBLL m_dathangbll = null;
        XuathangBLL m_xuathangbll = null;
        String values;
        SanphamBLL m_sanpham;
        NhanvienBLL m_nhanvienbll = new NhanvienBLL();
        public FormXuathang()
        {
            InitializeComponent();
           
        }

        private void btncheck_Click(object sender, EventArgs e)
        {
           
             if (check() == true)
             {
                 btncheck.Enabled = false;
                 MessageBox.Show("Kho còn hàng theo đơn đặt!");
                 dataGridView1.Enabled = false;
                 dataGridView2.Enabled = false;
                 visible(true);
                 Xuathang();
             }
             else
                 MessageBox.Show("Kho hết hàng theo đơn đặt!");
             
        }
        private String sinhma()
        {
            int i = m_xuathangbll.Demxuathang();
            String s = null;
            s = (i + 1).ToString();
            if (i < 9)
            {
                return "PX00" + s;
            }
            if ((i >= 9) && (i < 99))
            {
                return "PX0" + s;
            }
            else
                return "PX" + s;

        }
        private void Xuathang()
        {
            txtmaphieuxuat.Text = sinhma();
            txtmaphieuxuat.Enabled = false;
            txtmaphieudat.Enabled = false;
            txtmakhach.Enabled = false;
            cmbmanv.DataSource = m_nhanvienbll.Laydsnv();
            cmbmanv.DisplayMember = "manv";
            cmbmanv.ValueMember = "manv";
            
        }
        private bool check()
        {
            int j = dataGridView2.RowCount;
            m_xuathangbll = new XuathangBLL();
            float sum=0;
            m_sanpham = new SanphamBLL();
            for (int k = 0; k < j; k++)
            {

                String mahang1 = dataGridView2.Rows[k].Cells[0].Value.ToString();
                int soluong1 = int.Parse(dataGridView2.Rows[k].Cells[1].Value.ToString());
                if (m_xuathangbll.Kiemtrakho(mahang1, soluong1) == false)
                {
                    
                    return false;
                }
                sum = sum + m_sanpham.Laydongia(mahang1)*soluong1;
            }
            lbtongtien.Text = "Tổng giá trị đơn hàng: " + String.Format("{0:0,0}", sum) +" VNĐ";
            return true;
        }

        private void visible(bool a)
        {
            lbghichu.Visible = a;
            lbmadat.Visible = a;
            lbmakhach.Visible = a;
            lbmanv.Visible = a;
            lbmapx.Visible = a;
            lbngay.Visible = a;
            cmbmanv.Visible = a;
            txtmakhach.Visible = a;
            txtmaphieudat.Visible = a;
            txtmaphieuxuat.Visible = a;
            dateTimePicker1.Visible = a;
            richTextBox1.Visible = a;
            btnLuu.Visible = a;
        }

        private void FormXuathang_Load(object sender, EventArgs e)
        {
            dataGridView1.Enabled = true;
            dataGridView2.Enabled = true;
            m_dathangbll = new DathangBLL();
            dataGridView1.DataSource = m_dathangbll.Laydsdathang();
            dataGridView1.Show();

            visible(false);
        }

        private void btntimkiem_Click(object sender, EventArgs e)
        {
            m_dathangbll = new DathangBLL();
            dataGridView1.DataSource = m_dathangbll.Timdathang(txttimkiem.Text);
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
                
                values = dataGridView1.Rows[i].Cells[0].Value.ToString();
                m_dathangbll = new DathangBLL();
                dataGridView2.DataSource = m_dathangbll.Laychitiet(values);
                txtmaphieudat.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
                txtmakhach.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("bạn có muốn lưu?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                m_xuathangbll = new XuathangBLL();
                m_sanpham = new SanphamBLL();
                //lưu đơn đặt hàng
                String date = dateTimePicker1.Value.ToString("dd-MM-yyyy");


                m_xuathangbll.Themxuathang(txtmaphieuxuat.Text,date,txtmakhach.Text,richTextBox1.Text,cmbmanv.SelectedValue.ToString(),txtmaphieudat.Text);

                //lưu chi tiết đặt hàng.
                int j = dataGridView2.RowCount;

                for (int k = 0; k < j; k++)
                {
                    String mahang1 = dataGridView2.Rows[k].Cells[0].Value.ToString();



                    int soluong1 = Convert.ToInt16(dataGridView2.Rows[k].Cells[1].Value.ToString());
                    float dongia = m_sanpham.Laydongia(mahang1);
                    m_xuathangbll.Themchitietxuathang(txtmaphieuxuat.Text,mahang1,soluong1,dongia);

                    m_sanpham = new SanphamBLL();
                    m_sanpham.capnhatsoluongsanpham_tuphieuxuat(soluong1, mahang1);

                }
                m_dathangbll = new DathangBLL();
                m_dathangbll.Suadathang(txtmaphieudat.Text);
                MessageBox.Show("Lưu Thành Công!");
                FormXuathang_Load(sender, e);
                btncheck.Enabled = true;
                dataGridView1.Enabled = true;
                dataGridView2.Enabled = true;
                lbtongtien.Text = "";
                visible(false);
            }
            else
            {
                FormXuathang_Load(sender, e);
            }
        }

     

      

    }
}
